  //
//  HWTabBarViewController.m
//  黑马微博2期
//
//  Created by apple on 14-10-7.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "HBTabBarViewController.h"
#import "MKNumberBadgeView.h"
#import "HomeViewCtrl.h"
#import "HBIncomeCtrl.h"
#import "HBMineCtrl.h"
#import "HomeViewCtrl.h"
#import "InvitationViewCtrl.h"
/*
#import "LCMyAdviserCtrl.h"
#import "LCMyAgentCtrl.h"
#import "LCMyCenterCtrl.h"
#import "LCGetUnreadMsgManager.h"
*/




@interface HBTabBarViewController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) MKNumberBadgeView *badgeView;

@end

@implementation HBTabBarViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tabBar.barTintColor = [LCCommon getColor:@"2d2e32"];
    self.tabBar.opaque = NO;
    self.tabBar.translucent = NO;
    self.delegate = self;
    
   

    HomeViewCtrl *home = [[HomeViewCtrl alloc]init];
    [self addChildVc:home title:@"金桔" image:@"tabHB_notselect" selectedImage:@"tabHB_select"];

    HBIncomeCtrl *income = [[HBIncomeCtrl alloc]init];
    [self addChildVc:income title:@"收益" image:@"tabin_notselect" selectedImage:@"tabin_select"];

    HBMineCtrl *me = [[HBMineCtrl alloc]init];
    [self addChildVc:me title:@"我的" image:@"tabme_notselect" selectedImage:@"tabme_select"];
     

    self.badgeView = [self createBadgeViewInSuperView:self.tabBar andFrame:CGRectMake(SCREEN_WIDTH*0.85, 4, 30, 18)];
    self.badgeView.value = 0;

//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didReceiveRemoteNotification:) name:Notification_DidReceiveRomoteNoti object:nil];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shouldReceiveMessageCountNOtification:) name:Notification_getMessageCount object:nil];

    
}
/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */

- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage =[[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //if (IOS_8) {
        childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        childVc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 0);
    //}else{
      //  childVc.tabBarItem.image = [childVc.tabBarItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //}
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
//    textAttrs[NSForegroundColorAttributeName] = [LCCommon getColor:HIGH_COLOR];//[UIColor colorWithRed:(104)/255.0 green:(104)/255.0 blue:(104)/255.0 alpha:1.0];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [LCCommon getColor:@"fa9838"];//[UIColor colorWithRed:(219)/255.0 green:(69)/255.0 blue:(0)/255.0 alpha:1.0];
    
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    HBNavigationController *nav = [[HBNavigationController alloc] initWithRootViewController:childVc];
    nav.extendedLayoutIncludesOpaqueBars = NO;
    nav.edgesForExtendedLayout = UIRectEdgeNone;
    nav.automaticallyAdjustsScrollViewInsets = NO;
    //[self.tabBar setClipsToBounds:YES];
    
    // 添加为子控制器
    [self addChildViewController:nav];
    
    
}

- (void)didReceiveRemoteNotification:(NSNotification *)notification
{
    
    self.selectedIndex = 2;
    HBNavigationController *navi = [self.viewControllers lastObject];
    [navi popToRootViewControllerAnimated:NO];
}

- (void)shouldReceiveMessageCountNOtification:(NSNotification *)notification
{
//    self.badgeView.value = [[LCGetUnreadMsgManager sharedLCGetUnreadMsgManager].messageNumInfo.count integerValue];
//    self.badgeView.value = 1000;
    
}

- (MKNumberBadgeView *)createBadgeViewInSuperView:(UIView *)fatherView andFrame:(CGRect)rect
{
    LLog(@"rect:%@",NSStringFromCGRect(rect));
    MKNumberBadgeView *badgeView = [[MKNumberBadgeView alloc]initWithFrame:rect];
    
    badgeView.hideWhenZero=YES;
    badgeView.shadow=NO;
    badgeView.shine=NO;
    badgeView.font = [UIFont boldSystemFontOfSize:10.0f];
    badgeView.hidden = YES;
    
    [fatherView addSubview:badgeView];
    return badgeView;
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: YES animated: animated];
}

-(void) viewDidAppear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSInteger selectIndex = tabBarController.selectedIndex;
    if (selectIndex==1||selectIndex==2)
    {
        if (![HBLoginManager sharedHBLoginManager].hasLogined)
        {
            [HBLoginManager didLoginOut];
        }
    }
}

@end
